﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class admin_AddUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Main.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 75)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if (!IsPostBack)
        {
            txtJoinTime.Text = System.DateTime.Now.ToShortDateString();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        User u = new User();
        if (UserManager.FindByLoginName(txtLoginName.Text.Trim()) !=null)
        {
            ClientScript.RegisterStartupScript(this.GetType(),"","alert('该登陆名已有人使用！请更换其他名称！')",true);
            return;
        }
        u.LoginName = txtLoginName.Text.Trim();
        u.UserName = txtUserName.Text.Trim();
        u.Password = txtPassWord.Text.Trim();

        PopedomGroup pg = new PopedomGroup();
        pg.PopedomGroupId= Convert.ToInt32(DropDownList1.SelectedValue);
        u.PopedomGroup = pg;
        Section s = new Section();
        s.SectionId=Convert.ToInt32(DropDownList2.SelectedValue);
        u.Section = s;
        u.Wage =Convert.ToDecimal(txtWage.Text.Trim());
        Job j = new Job();
        j.JobId=Convert.ToInt32(DropDownList4.SelectedValue);
        u.Job = j;
        //ArrayList arrayValues = new ArrayList();
        //for (int i = 0; i < ListBox1.Items.Count;i++)
        //{
        //    if(ListBox1.Items[i].Selected)
        //    {
        //        arrayValues.Add(ListBox1.Items[i].Value);
        //    }
        //}
        //string manage = "";
        //for (int i = 0; i < arrayValues.Count;i++ )
        //{
        //    manage += arrayValues[i].ToString() + " ";
        //}
        u.ManageSection = "";
        User us = new User();
        us=(User)Session["user"];
        u.CreateUser = us;
        string path = Server.MapPath(@"/userFiles/" + u.LoginName);
        FileManage.CheckDir(path);
        u.SpacePath = path;
        UserInfo ui = new UserInfo();
        if (txtTelephone.Text != null && txtTelephone.Text != "")
        {
            ui.Telephone = txtTelephone.Text.Trim();
        }
        else
        {
            ui.Telephone = "";
        }
        if (txtAddress.Text != null && txtAddress.Text != "")
        {
            ui.Address = txtAddress.Text.Trim();
        }
        else
        {
            ui.Address = "";
        }
        if (mob.Text != null && mob.Text != "")
        {
            ui.Mob = mob.Text.Trim();
        }
        else
        {
            ui.Mob = "";
        }
        if (txtIDCard.Text != null && txtIDCard.Text != "")
        {
            ui.IDCard = txtIDCard.Text.Trim();
        }
        else
        {
            ui.IDCard = "";
        }
        ui.User = u;
        ui.Sex = Convert.ToBoolean(DropDownList5.SelectedValue);
        Nation n = new Nation();
        n.NationId=Convert.ToInt32(DropDownList6.SelectedValue);
        ui.Nation=n;
        if (txtEmail.Text != null && txtEmail.Text != "")
        {
            ui.Email = txtEmail.Text.Trim();
        }
        else
        {
            ui.Email = "";
        }
        if (txtFinishShool.Text != null && txtFinishShool.Text != "")
        {
            ui.FinishShool = txtFinishShool.Text.Trim();
        }
        else
        {
            ui.FinishShool = "";
        }
        ui.AcademicCertificate = Convert.ToInt32(DropDownList7.SelectedValue);
        if (txtFinishShoolTime.Text !=null && txtFinishShoolTime.Text != "")
        {
            ui.FinishShoolTime = Convert.ToDateTime(txtFinishShoolTime.Text);
        }
        else
        {
            ui.FinishShoolTime = Convert.ToDateTime("1970-1-1");
        }
        ui.JoinTime = Convert.ToDateTime(txtJoinTime.Text);
        bool b= UserManager.AddUser(u,ui);
        if(b)
        {
            FileManage.MkDir(path);
            ClientScript.RegisterStartupScript(this.GetType(),"","alert('添加成功！')",true);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('添加失败！请重试！')", true);
        }
    }
}
